package org.kjtc.controller;

import org.kjtc.commom.CommonUtil;
import org.kjtc.commom.XmlSerializer;
import org.kjtc.entity.BackLog;
import org.kjtc.entity.WeChatConfig;
import org.kjtc.entity.WeChatInfo;
import org.kjtc.mapper.TimerMapper;
import org.kjtc.service.TimerService;
import org.kjtc.web.JsonResult;
import org.kjtc.web.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.annotation.Resource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.Node;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.util.*;

@RestController
public class ConfigController {

    @Autowired
    private TimerMapper taskMapper;

    @Resource(name = "weChatInfo")
    private WeChatInfo WeChatInfo;

    @Autowired
    private TimerService timerService;

    @RequestMapping(value = "updateWeChatMessage")
    public JsonResult updateWeChatMessage(WeChatInfo weChatInfo)throws Exception{
        int num ;
        try {
            String filePath = CommonUtil.WECHAT_CONFIG_PATH;
            WeChatConfig.getInstance().setAccess(weChatInfo.getAccess());
            WeChatConfig.getInstance().setWeChatModuleID(weChatInfo.getWeChatModuleID());
            WeChatConfig.getInstance().setPushUrl(weChatInfo.getPushUrl());
            WeChatConfig.getInstance().setPushTime(weChatInfo.getPushTime());
            WeChatConfig.getInstance().setPushType(weChatInfo.getPushType());
            WeChatConfig.getInstance().setForwardUrl(weChatInfo.getForwardUrl());
            WeChatConfig.getInstance().setStatisticsTime(weChatInfo.getStatisticsTime());

            XmlSerializer<WeChatConfig> xSerializer = new XmlSerializer<WeChatConfig>();
            String strXML = xSerializer.marshallObj(WeChatConfig.class, WeChatConfig.getInstance(), "UTF-8");
            Tools.writeFile(filePath, strXML, true);
            num = 1;
            timerService.sendWorkDataEveryDay();
            timerService.insertBackLogDataEverybody();
        } catch (Exception e) {
            num = 0;
            e.printStackTrace();
        }
        return new JsonResult(num);
    }
    @RequestMapping("doGetSettingData")
    public JsonResult doGetSettingData(){

        File file = new File(CommonUtil.WECHAT_CONFIG_PATH);
        if (file.exists()) {
            XmlSerializer<WeChatConfig> xSerializer = new XmlSerializer<WeChatConfig>();
            try {
                WeChatInfo.setAccess(xSerializer.unMarshallXml(file, WeChatConfig.class).getAccess());
                WeChatInfo.setPushTime(xSerializer.unMarshallXml(file, WeChatConfig.class).getPushTime());
                WeChatInfo.setPushType(xSerializer.unMarshallXml(file, WeChatConfig.class).getPushType());
                WeChatInfo.setPushUrl(xSerializer.unMarshallXml(file, WeChatConfig.class).getPushUrl());
                WeChatInfo.setWeChatModuleID(xSerializer.unMarshallXml(file, WeChatConfig.class).getWeChatModuleID());
                WeChatInfo.setForwardUrl(xSerializer.unMarshallXml(file, WeChatConfig.class).getForwardUrl());
                WeChatInfo.setStatisticsTime(xSerializer.unMarshallXml(file, WeChatConfig.class).getStatisticsTime());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Map<String,Object> WeChatInfoMap = new HashMap<String,Object>();
        WeChatInfoMap.put("access",WeChatInfo.getAccess());
        WeChatInfoMap.put("pushTime",WeChatInfo.getPushTime());
        WeChatInfoMap.put("weChatModuleID",WeChatInfo.getWeChatModuleID());
        WeChatInfoMap.put("pushUrl",WeChatInfo.getPushUrl());
        WeChatInfoMap.put("pushType",WeChatInfo.getPushType());
        WeChatInfoMap.put("forwardUrl",WeChatInfo.getForwardUrl());
        WeChatInfoMap.put("statisticsTime",WeChatInfo.getStatisticsTime());

        return new JsonResult(WeChatInfoMap);
    }



}
